import { AgentMCPBridge, AgentConfig } from '../core/agent-mcp-bridge';

async function basicAgentExample() {
  console.log('=== Basic Agent Example ===');
  
  const agentConfig: AgentConfig = {
    name: 'DemoAgent',
    description: 'A simple demo agent for MCP integration',
    systemPrompt: 'You are a helpful assistant that can use tools to help users.',
    mcpClients: [
      {
        serverUrl: 'ws://localhost:8080/mcp',
        timeout: 30000,
        retryCount: 3
      }
    ]
  };

  const agent = new AgentMCPBridge(agentConfig);
  
  try {
    await agent.initialize();
    
    console.log('Agent Status:', agent.getStatus());
    
    const response = await agent.processMessage({
      role: 'user',
      content: 'Hello! Can you help me check the weather?'
    });
    
    console.log('Agent Response:', response);
    
  } catch (error) {
    console.error('Example failed:', error);
  } finally {
    await agent.shutdown();
  }
}

async function weatherAgentExample() {
  console.log('=== Weather Agent Example ===');
  
  const agentConfig: AgentConfig = {
    name: 'WeatherAgent',
    description: 'Specialized agent for weather information',
    systemPrompt: 'You are a weather assistant. You can provide weather information using available tools.',
    mcpClients: [
      {
        serverUrl: 'ws://localhost:8080/mcp',
        timeout: 30000,
        retryCount: 3
      }
    ]
  };

  const agent = new AgentMCPBridge(agentConfig);
  
  try {
    await agent.initialize();
    
    const userQuestions = [
      'What\'s the weather like in Beijing?',
      'Tell me about the current weather conditions.',
      'Can you check if it\'s raining in Shanghai?'
    ];
    
    for (const question of userQuestions) {
      console.log(`\nUser: ${question}`);
      
      const response = await agent.processMessage({
        role: 'user',
        content: question
      });
      
      console.log(`Assistant: ${response.content}`);
      
      await new Promise(resolve => setTimeout(resolve, 2000));
    }
    
  } catch (error) {
    console.error('Weather example failed:', error);
  } finally {
    await agent.shutdown();
  }
}

async function multiServerExample() {
  console.log('=== Multi-Server Agent Example ===');
  
  const agentConfig: AgentConfig = {
    name: 'MultiServerAgent',
    description: 'Agent connecting to multiple MCP servers',
    systemPrompt: 'You are a versatile assistant with access to multiple tool servers.',
    mcpClients: [
      {
        serverUrl: 'ws://localhost:8080/weather',
        timeout: 30000,
        retryCount: 3
      },
      {
        serverUrl: 'ws://localhost:8081/calculator',
        timeout: 30000,
        retryCount: 3
      },
      {
        serverUrl: 'ws://localhost:8082/database',
        timeout: 30000,
        retryCount: 3
      }
    ]
  };

  const agent = new AgentMCPBridge(agentConfig);
  
  try {
    await agent.initialize();
    
    console.log('Available Tools:');
    const tools = agent.getAvailableTools();
    tools.forEach(tool => {
      console.log(`- ${tool.name}: ${tool.description}`);
    });
    
    console.log('\nAvailable Resources:');
    const resources = agent.getAvailableResources();
    resources.forEach(resource => {
      console.log(`- ${resource.uri}: ${resource.description || 'No description'}`);
    });
    
    const complexTasks = [
      'Calculate 15 * 23 + 47',
      'Get weather for Beijing and calculate the temperature in Fahrenheit',
      'What time is it and what\'s the weather like?'
    ];
    
    for (const task of complexTasks) {
      console.log(`\nTask: ${task}`);
      
      const response = await agent.processMessage({
        role: 'user',
        content: task
      });
      
      console.log(`Result: ${response.content}`);
      
      await new Promise(resolve => setTimeout(resolve, 3000));
    }
    
  } catch (error) {
    console.error('Multi-server example failed:', error);
  } finally {
    await agent.shutdown();
  }
}

async function errorHandlingExample() {
  console.log('=== Error Handling Example ===');
  
  const agentConfig: AgentConfig = {
    name: 'ErrorTestAgent',
    description: 'Agent for testing error handling',
    systemPrompt: 'You are a helpful assistant that handles errors gracefully.',
    mcpClients: [
      {
        serverUrl: 'ws://localhost:9999/invalid', // Invalid server
        timeout: 5000,
        retryCount: 2
      },
      {
        serverUrl: 'ws://localhost:8080/mcp', // Valid server
        timeout: 30000,
        retryCount: 3
      }
    ]
  };

  const agent = new AgentMCPBridge(agentConfig);
  
  try {
    await agent.initialize();
    
    console.log('Agent status after initialization:', agent.getStatus());
    
    const testMessages = [
      'This should work even with some failed connections',
      'Can you try to use a tool that might not exist?',
      'What happens if you request something invalid?'
    ];
    
    for (const message of testMessages) {
      console.log(`\nTesting: ${message}`);
      
      const response = await agent.processMessage({
        role: 'user',
        content: message
      });
      
      console.log(`Response: ${response.content}`);
      
      await new Promise(resolve => setTimeout(resolve, 2000));
    }
    
  } catch (error) {
    console.error('Error handling example failed:', error);
  } finally {
    await agent.shutdown();
  }
}

export {
  basicAgentExample,
  weatherAgentExample,
  multiServerExample,
  errorHandlingExample
};